package com.hs.room.repository

import android.content.Context
import com.hs.room.AppDatabase
import com.hs.room.model.archive.DevCabinet
import com.hs.room.model.archive.DevReader
import com.hs.room.repository.service.IDevRepository
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.emitAll
import kotlinx.coroutines.flow.flow
import javax.inject.Inject

/**
 * 设备配置相关仓库
 */
class DevRepository @Inject constructor(
    @ApplicationContext var context: Context,
    val db: AppDatabase
) : IDevRepository {

    override suspend fun insert(d: DevCabinet) {
        db.devCabinetDao().insert(d)
    }

    override suspend fun insert(d: DevReader) {
        db.devReaderDao().insert(d)
    }

    override suspend fun update(d: DevCabinet) {
        db.devCabinetDao().update(d)
    }

    override suspend fun update(d: DevReader) {
        db.devReaderDao().update(d)
    }

    override suspend fun getDevCabinet(limit: Int, offset: Int): Flow<List<DevCabinet>> {
        return flow {
            emitAll(db.devCabinetDao().get(offset, offset * limit))
        }.catch { e ->
            // 进行异常处理
            println("Error occurred while fetching getType data: $e")
        }
    }

    override suspend fun getDevCabinet(): Flow<List<DevCabinet>> {
        return flow {
            emitAll(db.devCabinetDao().get())
        }.catch { e ->
            // 进行异常处理
            println("Error occurred while fetching getType data: $e")
        }
    }

    override fun getDevCabinetBoardId(): List<Int> {
        return db.devCabinetDao().getBoardId()
    }

    override suspend fun getLamp(): Flow<List<DevCabinet>> {
        return flow {
            emitAll(db.devCabinetDao().getLamp())
        }.catch { e ->
            // 进行异常处理
            println("Error occurred while fetching getType data: $e")
        }
    }

    override suspend fun getDevReader(limit: Int, offset: Int): Flow<List<DevReader>> {
        return flow {
            emitAll(db.devReaderDao().get(offset, offset * limit))
        }.catch { e ->
            // 进行异常处理
            println("Error occurred while fetching getType data: $e")
        }
    }

    override suspend fun getDevReaderName(): Flow<List<String>> {
        return flow {
            emitAll(db.devReaderDao().getName())
        }.catch { e ->
            // 进行异常处理
            println("Error occurred while fetching getType data: $e")
        }
    }

    override suspend fun getDevCabinetName(): Flow<List<String>> {
        return flow {
            emitAll(db.devCabinetDao().getName())
        }.catch { e ->
            // 进行异常处理
            println("Error occurred while fetching getType data: $e")
        }
    }

    override suspend fun getDevReaderID(): Flow<List<Int>> {
        return flow {
            emitAll(db.devReaderDao().getIds())
        }.catch { e ->
            // 进行异常处理
            println("Error occurred while fetching getType data: $e")
        }
    }

    override fun countDevCabinet(): Int {
        return db.devCabinetDao().count()
    }

    override fun countDevReader(): Int {
        return db.devReaderDao().count()
    }

    override suspend fun delete(d: DevCabinet) {
        db.devCabinetDao().delete(d)
    }

    override suspend fun delete(d: DevReader) {
        db.devReaderDao().delete(d)
    }
}